package logic.nodes.collision;

import logic.nodes.MoveForwardController;

import com.jme.input.controls.GameControl;
import com.jme.math.FastMath;
import com.jme.math.Vector3f;

public class ImpulseController extends MoveForwardController {
	
	private static final long serialVersionUID = 1L;
	
	protected final float MAX_SPEED = 0.4f;
	protected static final float BREAKING = 0.85f;
		
	protected Vector3f impulseDir;
	protected CollidableNode collNode;
	protected float initSpeed;
	
	public ImpulseController(CollidableNode node, GameControl forwardControl, 
			float maxSpeed, GameControl breakControl, Vector3f impulseDir) {
		super(node, forwardControl, maxSpeed, 0f, 1f, breakControl, BREAKING);
		this.impulseDir = impulseDir.normalize();
		currentSpeed = maxSpeed;
		
		if(FastMath.abs(currentSpeed) > MAX_SPEED) 
			currentSpeed = (currentSpeed < 0f ? -MAX_SPEED : MAX_SPEED);
		
		collNode = node;
		initSpeed = maxSpeed;
	}
	
	@Override
	public void update(float time) {
		if(getCurrentSpeed() <= 0f) collNode.removeImpulseController(this);
		else super.update(time);
	}
	
	@Override
	protected void move(float tpf) {
		node.getLocalTranslation().addLocal(impulseDir.mult(currentSpeed * tpf));
	}
	
	@Override
	public Vector3f getMoveDirection() { return impulseDir; }
	
	public float getInitSpeed() { return initSpeed; }
}